Java, Classpath, Classloading => Multiple Versions of the same jar/project?

Classloader related problems are a quite complex matter. You should in any case keep in mind some facts.

Classloader related problems are a quite complex matter. You should in any case keep in mind some facts: Classloaders in an application are usually more than a single one. The bootstrap class loader detegates to the appropriate.

When you instantiate a new class the more specific classloader is invoked. If he do not find a reference to the class you are trying to load, he delegates to his parent, and so on, 'till you get to the bootstrap class loader. If none of them find a reference to the class you are trying to load you get a ClassNotFoundException.

If you have two classes with the same binary name, searchable by the same classloader, and you want to know what of them you are loading, you can only inspect the way that specific classloader tries to resolve a class name. According to the java language specification, there is not a uniqueness constraint for a class binary name, but as far as I can see, it should be unique for each classloader. I can figure out a way to load two classes with the same binary name, and it involves to have them loaded (and all their dependencies) by two different classloaders overriding default behaviour.

A rough example: ClassLoader loaderA = new MyClassLoader(libPathOne); ClassLoader loaderB = new MyClassLoader(libPathTwo); Object1 obj1 = loaderA. LoadClass("first.class.binary. Name", true) Object2 obj2 = loaderB.

LoadClass("second.class.binary. Name", true); I always found classloader customization a tricky task. I'd rather suggest to avoid multiple incompatible dependencies...

Classloader will load classes from the jar that happened to be in the classpath first. Normally, incompatible versions of library will have difference in packages, But in unlikely case they really incompatible and can't be replaced with one - try jarjar.

Each classload picks exactly one class. Usually the first one found. OSGi aims to solve the problem of multiple versions of the same jar.

Equinox and Apache Felix are the common open-source implementations for OSGi.

Classloaders load class on demand. This means that the class required first by your application and related libraries would be loaded before other classes; the request to load the dependent classes is typically issued during the loading and linking process of a depending class. You are likely to encounter LinkageErrors stating that duplicate class definitions have been encountered for classloaders typically do not attempt to determine which class should be loaded first (if there are two or more classes of the same name present in the classpath of the loader).

Sometimes, the classloader will load the first class occurring in the classpath and ignore the duplicate classes, but this depends on the implementation of the loader. The recommended practice to resolve such kind of errors is to utilize a separate classloader for each set of libraries that have conflicting dependencies. That way, if a classloader attempts to load classes from a library, the dependent classes would be loaded by the same classloader that does not have access to the other libraries and dependencies.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions